<template>
    <div class="searchwrap">
        <!-- 顶部导航 -->
        <div>
            <div style="height: 2.56rem"></div>
            <div class="jsearch_header headeractive headfixed">
                <div class="jcontent">
                    <div class="ds-in-bl search">
                        <div class="j_searlef">
                            <img
                                src="../../assets/newimg/index/home_icon_dingwei@2x.png"
                                alt=""
                            />

                            <span> 北京市 </span>
                        </div>

                        <div class="sea-box">
                            <span @click="searchStart()"></span>
                            <form action="" method="post">
                                <div class="jsear-input">
                                    <input
                                        type="text"
                                        class="search_text"
                                        v-model="searchVal"
                                        @input="searchValChange()"
                                        placeholder="请输入要搜索的内容"
                                    />
                                </div>
                            </form>
                            <i
                                @click="
                                    (searchVal = ''), (isShopandHistory = true)
                                "
                            ></i>
                        </div>
                        <div
                            class="j_searchrir"
                            @click="$router.push('/message')"
                        >
                            <img
                                src="../../assets/newimg/index/home_icon_xiaoxi@2x.png"
                                alt=""
                            />

                            <span>2</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 没有搜索内容时候 -->
        <div
            class="lb_showhide se_shien p"
            v-if="isShopandHistory"
            style="display: block"
        >
            <div
                class="j_search_history"
                v-if="JSON.stringify(historyList) != '[]'"
            >
                <div class="j_searchtitle">
                    <span>搜索记录</span>
                    <i class="j_empty_rem" @click="clearHistort">清除记录</i>
                </div>
                <div class="history-s">
                    <ul class="history-list">
                        <li
                            v-for="(item, index) in historyList"
                            :key="index"
                            @click="searchStart(item)"
                        >
                            <a>{{ item }}</a>
                        </li>
                    </ul>
                </div>
            </div>

            <div
                class="j_Search_empty"
                v-if="JSON.stringify(historyList) == '[]'"
            >
                <span>暂无历史记录</span>
            </div>

            <div class="" style="padding-bottom: 4rem">
                <div class="j_searchtitle" style="justify-content: flex-start">
                    <span>热门搜索</span>
                    <img
                        src="../../assets/newimg/index/home_icon_huo@2x.png"
                        alt=""
                    />
                </div>
                <div>
                    <ul class="history-s">
                        <li>
                            <a class="ht">手机</a>
                        </li>
                        <li>
                            <a>小米</a>
                        </li>
                        <li>
                            <a>iphone</a>
                        </li>
                        <li>
                            <a>三星</a>
                        </li>
                        <li>
                            <a>华为</a>
                        </li>
                        <li>
                            <a>冰箱</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- 排序按钮 -->
        <nav class="storenav p search_list_dump" v-if="!isShopandHistory">
            <ul>
                <div v-for="(itemr, i) in navCheckDowList" :key="i">
                    <li @click="navCheckClick(itemr)">
                        <span class="lb">{{ itemr.name }}</span>
                        <i
                            :class="{
                                pd: itemr.checkType == 0,
                                pc: itemr.checkType == 0 && itemr.ischeck,
                                pr: itemr.checkType == 1,
                                bpr1: itemr.checkType == 1 && itemr.ischeck,
                                fitter: itemr.checkType == 2,
                                listorimg: itemr.checkType == 3,
                            }"
                        ></i>
                    </li>
                    <div
                        class="shadow"
                        style="z-index: 99; top: 4.48rem"
                        v-if="itemr.ischeck && itemr.checkType == 0"
                        @click="itemr.ischeck = !itemr.ischeck"
                    ></div>

                    <div class="j_sel_option" v-show="itemr.ischeck">
                        <ul class="j_sel_optionul">
                            <li
                                :class="{
                                    j_sel_optionactive:
                                        itemr.checkName == item.nameli,
                                }"
                                v-for="(item, index) in itemr.checkList"
                                :key="index"
                                @click="changenavCheckFn(itemr, item)"
                            >
                                <span>{{ item.nameli }}</span>
                            </li>
                        </ul>
                    </div>
                </div>
                <!-- <li class="">
                    <a>
                        <span class="dq">销量</span>
                        <i class="pr"></i>
                    </a>
                </li> -->
                <!-- <li class="">
                    <a>
                        <span class="jg">价格 </span>
                        <i class="pr"></i>
                    </a>
                </li>
                <li @click="slideSearch = !slideSearch">
                    <span class="sx">筛选</span>
                    <i class="fitter"></i>
                </li>
                <li>
                    <i class="listorimg"></i>
                </li> -->
            </ul>
        </nav>

        <!-- 更多筛选 -->
        <div
            class="shadow"
            v-if="slideSearch"
            @click="slideSearch = !slideSearch"
        ></div>

        <transition :name="transitionName">
            <div
                class="screen_wi router"
                v-show="slideSearch"
                style="display: block; width: 13.87rem"
            >
                <div class="title">筛选</div>
                <ul class="ul ul1">
                    <li class="choose">
                        <span class="ch_dg">
                            显示全部<input
                                type="hidden"
                                class="sel"
                                value="all"
                            />
                        </span>
                    </li>
                    <li>
                        <span>
                            仅看包邮<input type="hidden" value="free_post" />
                        </span>
                    </li>
                    <li>
                        <span>
                            仅看有货<input type="hidden" value="store_count" />
                        </span>
                    </li>
                    <li>
                        <span>
                            促销商品<input type="hidden" value="prom_type" />
                        </span>
                    </li>
                </ul>
                <div class="line"></div>
                <div class="tit">价格</div>
                <ul class="ul ul2">
                    <!--价格筛选-s-->
                    <li
                        data-href="/index.php/mobile/Goods/search/id/0/q/手机/price/0-1198"
                    >
                        1198元以下
                    </li>
                    <li
                        data-href="/index.php/mobile/Goods/search/id/0/q/手机/price/1198-2396"
                    >
                        1198-2396元
                    </li>
                    <li
                        data-href="/index.php/mobile/Goods/search/id/0/q/手机/price/2396-3594"
                    >
                        2396-3594元
                    </li>
                    <li
                        data-href="/index.php/mobile/Goods/search/id/0/q/手机/price/4792-5990"
                    >
                        4792-5990元
                    </li>
                </ul>
                <div class="input">
                    <input class="val1" placeholder="最低价" type="number" />
                    <text>-</text>
                    <input class="val2" placeholder="最高价" type="number" />
                </div>
                <div class="bottom">
                    <p class="reset">重置</p>
                    <p
                        class="ds-in-bl suce_ok"
                        @click="slideSearch = !slideSearch"
                    >
                        确定
                    </p>
                </div>
            </div>
        </transition>

        <!-- 商品列表 -->
        <div
            id="goods_list"
            :class="{ addimgchan: showType }"
            v-if="!isShopandHistory"
        >
            <!--商品-s-->
            <div class="orderlistshpop p">
                <div class="maleri30">
                    <div class="sc_list se_sclist">
                        <div class="shopimg fl">
                            <a class="item"
                                ><img
                                    src="/public/upload/goods/thumb/49/goods_thumb_49_0_400_400.png"
                            /></a>
                        </div>
                        <div class="deleshow fr">
                            <div class="deletes">
                                <a class="item"
                                    ><span class="similar-product-text fl"
                                        >三星（SAMSUNG）存储卡128GB 读速100MB/s
                                        写速90MB/s 4K Class10 高速TF卡（Micro
                                        SD卡）红色plus升级版</span
                                    ></a
                                >
                            </div>
                            <div class="prices">
                                <p class="sc_pri fl">
                                    <span>￥</span><b>219.00</b>
                                </p>
                            </div>
                            <p class="weight">
                                <span>已售3件</span>
                                <span goods_id="49" class="add_cart"
                                    ><img
                                        src="../../assets/images/red-icon-cart.png"
                                        alt=""
                                /></span>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="orderlistshpop p">
                <div class="maleri30">
                    <div class="sc_list se_sclist">
                        <div class="shopimg fl">
                            <a class="item"
                                ><img
                                    src="/public/upload/goods/thumb/79/goods_thumb_79_0_400_400.png"
                            /></a>
                        </div>
                        <div class="deleshow fr">
                            <div class="deletes">
                                <a class="item"
                                    ><span class="similar-product-text fl"
                                        >【新品上市】Samsung/三星 Galaxy S9
                                        SM-G9600/DS 全网通 4G手机</span
                                    ></a
                                >
                            </div>
                            <div class="prices">
                                <p class="sc_pri fl">
                                    <span>￥</span><b>2999.00</b>
                                </p>
                            </div>
                            <p class="weight">
                                <span>已售0件</span>
                                <span goods_id="79" class="add_cart"
                                    ><img
                                        src="../../assets/images/red-icon-cart.png"
                                        alt=""
                                /></span>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
            <!--商品-e-->
        </div>

        <div class="zhanwei"></div>
    </div>
</template>

<script>
// import { axiosPost } from '../../axios';
// import { Toast } from 'vant';
export default {
    name: '',
    data() {
        return {
            searchVal: '', // 搜索文本
            historyList: [], // 历史搜索记录列表
            navCheckDowList: [
                {
                    name: '综合',
                    checkName: '综合',
                    checkIndex: 0,
                    checkType: 0,
                    ischeck: false,
                    checkList: [
                        {
                            id: 0,
                            nameli: '综合',
                        },
                        {
                            id: 1,
                            nameli: '新品',
                        },
                        {
                            id: 2,
                            nameli: '评价',
                        },
                    ],
                },
                {
                    name: '销量',
                    checkName: true,
                    checkIndex: 0,
                    checkType: 1,
                    ischeck: false,
                    checkList: [],
                },
                {
                    name: '价格',
                    checkName: true,
                    checkIndex: 0,
                    checkType: 1,
                    ischeck: false,
                    checkList: [],
                },
                {
                    name: '筛选',
                    checkName: true,
                    checkIndex: 0,
                    checkType: 2,
                    ischeck: false,
                    checkList: [],
                },
                {
                    name: '',
                    checkName: 'true',
                    checkIndex: 0,
                    checkType: 3,
                    ischeck: false,
                    checkList: [],
                },
            ],

            slideSearch: false, // 侧边搜索面板是否显示

            transitionName: 'slide-left', // 默认动态路由变化为slide-right

            showType: false, // 商品列表展示方式

            isShopandHistory: true, // 展示商品结果还是搜索历史
        };
    },
    computed: {},
    mounted() {
        this.historySearch(); // 读取搜索历史记录
    },
    watch: {
        slideSearch(to) {
            if (to) {
                this.transitionName = 'slide-left';
            } else {
                this.transitionName = 'slide-right';
            }
        },
    },
    methods: {
        // 点击 nav 选项操作
        navCheckClick(itemr) {
            // 筛选
            if (itemr.checkType == 2) {
                this.slideSearch = !this.slideSearch;
                return;
            }

            // 展示方式
            if (itemr.checkType == 3) {
                this.showType = !this.showType;
                return;
            }

            // 其他
            this.navCheckDowList = this.navCheckDowList.map((el) => {
                switch (el.checkType) {
                    case 0: // 没有子选项
                        if (!itemr.ischeck) {
                            el.ischeck = false;
                            if (el.name == itemr.name) {
                                el.ischeck = true;
                            }
                        } else {
                            el.ischeck = false;
                        }
                        break;
                    case 1:
                        if (el.name == itemr.name) {
                            el.ischeck = !el.ischeck;
                        }

                        break;
                    default:
                        break;
                }

                return el;
            });
        },
        // 导航子选项点击
        changenavCheckFn(itemr, item) {
            this.navCheckDowList = this.navCheckDowList.map((el) => {
                if (itemr.name == el.name) {
                    el.name = item.nameli;
                    el.checkName = item.nameli;
                    el.checkIndex = item.id;
                    el.ischeck = false;
                }
                return el;
            });
        },
        // 搜索值变化事件
        searchValChange() {},
        // 开始搜索
        searchStart(value = '') {
            if (value) {
                this.searchVal = value;
            }
            if (!this.searchVal) {
                return;
            }

            // console.log(this.searchVal);
            this.isShopandHistory = !this.isShopandHistory;

            this.historySearch(true); // 储存搜索历史记录
        },
        // 清空历史搜索记录
        clearHistort() {
            this.historyList = [];
            localStorage.removeItem('searchList');
        },
        // 储存和读取历史搜索记录
        historySearch(type) {
            let history = localStorage.searchList;
            if (history) {
                this.historyList = JSON.parse(history);
            } else {
                this.historyList = [];
            }
            if (type) {
                this.historyList.push(this.searchVal);
                this.historyList = [...new Set(this.historyList)];
                localStorage.searchList = JSON.stringify(this.historyList);
            }
        },
    },
};
</script>

<style lang="less" scope>
.searchwrap {
    // 选择列表
    .j_sel_option {
        border-top: 1px solid #e0e0e0;
        position: absolute;
        left: 0;
        top: 1.91rem;
        max-height: 5rem;
        overflow-y: auto;
        width: 100%;
        background: #fff;
        z-index: 1000;

        .j_sel_optionactive {
            color: #fe333d;
        }

        li {
            float: none;
            height: 1.5667rem;
            line-height: 1.5667rem;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 0.85rem;
            width: 100%;
        }
    }

    .storenav {
        position: fixed;
        top: 2.56rem;
    }
    .router {
        transition: all 0.3s ease;
        position: absolute;
    }

    .slide-left-enter,
    .slide-right-leave-active {
        opacity: 0;
        -webkit-transform: translate(100%, 0);
        transform: translate(100%, 0);
    }

    .slide-left-leave-active,
    .slide-right-enter {
        opacity: 0;
        -webkit-transform: translate(-100%, 0);
        transform: translate(-100% 0);
    }
    .shadow {
        width: 100%;
        height: 100%;
        position: fixed;
        left: 0;
        top: 0;
        z-index: 999999;
        background: rgba(0, 0, 0, 0.5);
    }

    .search_pl {
        margin: 0;
    }

    .search_pl .search2 .le_inp input {
        width: 11.95rem;
        height: 1.28rem;
        background-color: transparent;
        border-radius: 0.62rem;
        border: none;
        font-size: 0.47rem;
        margin: 0 0.2rem;
    }

    .search_pl .search2 .ri_ss {
        width: 1.75rem;
        height: 1.28rem;
        background-color: #f75656;
        border-radius: 0.64rem;
        font-size: 0.51rem;
        line-height: 1.28rem;
        margin-top: 0.3rem;
        margin-left: 0.53rem;
        letter-spacing: 1px;
    }

    .search_pl .search2 .ri_ss a {
        color: #ffffff;
    }

    .ret {
        width: 1.28rem;
        height: 1.28rem;
        background: url(../../assets/images/return.png) center center no-repeat;
        background-size: 0.7rem 0.7rem;
    }

    .near-le-ri {
        padding: 0;
        margin-bottom: 0.55rem;
    }

    .ovfl {
        overflow: hidden;
        margin: 0;
        height: 2.3467rem;
        padding: 0;
        background: linear-gradient(0deg, #ff3b3b 0%, #fe2241 100%);
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .se_shien {
        padding: 0 !important;
        margin-bottom: 1.64rem;
    }

    .near-le-ri span {
        color: #131313;
        font-size: 0.64rem;
        font-weight: bold;
        margin-top: 0.853rem;
        letter-spacing: 1px;
    }

    .lb_showhide ul li a {
        width: 2.56rem;
        height: 1.07rem;
        background-color: #f5f5f5;
        border-radius: 0.53rem;
        border: none;
        font-size: 0.512rem;
        padding: 0.21333rem 0.64rem;
    }

    .lb_showhide ul li {
        margin-right: 0.47rem;
        height: 1.07rem;
        float: left;
        border: 0;
        width: auto;
    }

    .hs-title {
        font-size: 0.64rem;
        color: #131313;
        line-height: 0.66rem;
        font-weight: bold;
        letter-spacing: 1px;
        margin-bottom: 0.3rem;
    }

    .history-s {
        padding-left: 0.64rem;
        overflow: hidden;
    }

    .history-s::after {
        clear: both;
    }

    .history-s li {
        width: 14.72rem;
        height: 1.92rem;
        line-height: 1.92rem;
        font-size: 0.55rem;
        border-bottom: 0.033rem solid #e5e5e5;
    }

    .del-h {
        width: 1.79rem;
        height: 0.77rem;
        border-radius: 0.38rem;
        border: solid 0.02rem #e5e5e5;
        font-size: 0.47rem;
        display: inline-block;
        float: right;
        color: #999999;
        line-height: 0.77rem;
        text-align: center;
        margin-top: 0.58rem;
    }

    .j_searchrir {
        position: relative;
    }

    .j_searchrir img {
        width: 0.8747rem;
    }

    .j_searchrir span {
        display: inline-block;
        width: 0.64rem;
        height: 0.64rem;
        border-radius: 0.32rem;
        background: #ff9a1e;
        font-size: 0.4907rem;
        font-weight: 600;
        color: #ffffff;
        text-align: center;
        line-height: 0.64rem;
        position: absolute;
        right: -0.3rem;
        top: -0.3rem;
    }

    .search2 {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #sourch_form {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .search_pl .search2 .le_inp {
        float: none;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 11.0933rem;
        height: 1.28rem;
        border-radius: 0.64rem;
        background: #ffffff;
        margin: 0 0.807rem;
    }

    .le_inp h2 {
        padding-right: 0.4907rem;
        height: 1.28rem;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0;
    }

    .le_inp h1 {
        padding-left: 0.4907rem;
        height: 1.28rem;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0;
    }

    .le_inp h1 img {
        width: 0.6613rem;
        height: 0.6613rem;
    }

    .le_inp h2 img {
        width: 0.5333rem;
    }

    .se_shien {
        width: 15.1467rem;
        margin: 0.64rem auto 0;
        border-radius: 0.0427rem;
    }

    .j_searchtitle {
        padding: 0.64rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .j_searchtitle span {
        font-size: 0.64rem;
        font-weight: 500;
        color: #000000;
    }

    .j_searchtitle img {
        margin-left: 0.2347rem;
        width: 0.4267rem;
    }

    .j_searchtitle i {
        font-size: 0.5547rem;
        font-weight: 400;
        color: #999999;
    }

    .j_Search_empty {
        height: 6.4rem;
        text-align: center;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .j_Search_empty span {
        font-size: 0.576rem;
        font-weight: 400;
        color: #666666;
        line-height: 0.8107rem;
    }

    #goods_list {
        margin-top: 1.92rem;
    }
}
</style>
